using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UpgradeSentientWeapon:Upgrade {

	protected override void ReloadStartEventHandler(object _sender,ReloadStartEventArgs e) {
		base.ReloadStartEventHandler(_sender,e);
		if(!IfReactWeaponEvent(_sender,e)) return;
		if(weapon.owner.energy<=0) return;

		int ammoCount = weapon.weapon.currentClip.content.Count;
		if(ammoCount>=weapon.weapon.currentClip.clipSize) return;
		weapon.weapon.currentClip.content.Clear();

		GameObject prefab = Resources.Load<GameObject>("SentientWeapon");
		Mob newMob = MobManager.Create(prefab,weapon.muzzle.position);

		if(weapon.isPlayer)
			newMob.gameObject.AddComponent(typeof(PlayerMinionController));
		WeaponInstance newWeapon = new WeaponInstance(weapon.weaponInstance.weaponData);

		newWeapon.weaponLevel=weapon.weaponInstance.weaponLevel;
		newWeapon.baseUpgradeSlotCount=weapon.weaponInstance.baseUpgradeSlotCount;

		foreach(var i in weapon.weaponInstance.upgradeInstances){
			newWeapon.TryAddUpgrade(new UpgradeInstance(i.upgradeData),newWeapon.upgradeInstances.Last);
		}


		newWeapon.currentClip=new ClipStats();
		newWeapon.currentClip.clipSize=ammoCount;
		newWeapon.currentClip.FillWithDefault(int.MaxValue);
		newWeapon.weaponLevel=weapon.weaponInstance.weaponLevel;
		weapon.owner.energy-=ammoCount*0.166667f;

		newMob.GetComponentInChildren<WeaponSlot>().weaponInstance=newWeapon;
		newMob.faction=weapon.owner.faction;
		var link = newMob.gameObject.AddComponent<EnergyConsumptionLink>();
		link.targetMob=weapon.owner;
	}

}
